package com.javen.controller;

import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javan.util.ObjtoLayJson;
import com.javen.model.Bao;
import com.javen.model.Page;
import com.javen.service.BaoService;

@Controller//默认情况下返回视图
@RequestMapping("/baoyang")

public class BaoController {
	@Autowired
	private BaoService baoService;
	
	@RequestMapping("/add")//增加
	@ResponseBody
	public int add(HttpServletRequest request) {
		String carOwner=request.getParameter("carOwner");
		String model=request.getParameter("model");
		String record=request.getParameter("record");
		
		Bao bao=new Bao();
		bao.setCarOwner(carOwner);
		bao.setRecord(record);
		bao.setModel(model);
		int code=baoService.add(bao);
		return code;
	}
	@RequestMapping("/delete")//删除
	@ResponseBody
	public int delete(HttpServletRequest request) {
		String idString =request.getParameter("id");
		Integer idInteger=Integer.valueOf(idString);
		int code=baoService.delete(idInteger);
		return code;
	}
	


	@RequestMapping(value="/update", method=RequestMethod.GET, produces = "text/plain;charset=utf-8")
	@ResponseBody
	public String updateByPrimaryKey(HttpServletRequest request) {

		String idString =request.getParameter("id");
		Integer idInteger=Integer.valueOf(idString);
		String carOwner=request.getParameter("carOwner");
		String model=request.getParameter("model");
		String record=request.getParameter("record");
		Bao bao=new Bao();
		bao.setId(idInteger);
		bao.setCarOwner(carOwner);
		bao.setRecord(record);
		bao.setModel(model);
		int code =baoService.update(bao);

		return "{\"code\":\""+ code +"\",\"msg\":\"修改成功\"}";

	}
	//分页查询该车主的保养记录
	@RequestMapping(value="/select", method=RequestMethod.GET, produces = "text/plain;charset=utf-8")
	@ResponseBody
	public String select(HttpServletRequest request) throws Exception {
		HttpSession session=request.getSession();
		session.getAttribute("carOwner");
		System.out.println(session.getAttribute("carOwner"));
		Page page = new Page();
		//接收page的limit和page offset
		String pageString = request.getParameter("page");
		Integer pageInteger = Integer.valueOf(pageString);
		String limitString = request.getParameter("limit");
		Integer limitInteger = Integer.valueOf(limitString);
		String carOwner = request.getParameter("carOwner");
		page.setPage(pageInteger-1);
		page.setLimit(limitInteger);
		page.setOffset((pageInteger-1)*limitInteger);
		page.setCarOwner(carOwner);
		List<Bao> baos=baoService.select(page);
		String[] colums= {"id","carOwner","model","record"};
		String data=ObjtoLayJson.ListtoJson(baos, colums);
		System.out.println(data);

		return data ;

	}
	//根据车主计算车辆保养信息数量
	@RequestMapping(value="/countAllByCarOwner", method=RequestMethod.GET,produces = "text/plain;charset=utf-8")  
	@ResponseBody
	public String countAllByCarOwner(HttpServletRequest request) {
		String carOwner = request.getParameter("carOwner");
		int count = baoService.countAllByCarOwner(carOwner);
		return "{\"code\":\"200\",\"count\":\""+count+"\"}";
	}
}
